package com.study;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import java.util.List;

/**
 * @version 1.0
 * @Author: 许啸林
 * @Date: 2021/10/12 15:26
 */
public class ActivitiDemo {
    /**
     * 流程部署
     */
    @Test
    public void testDeployement() {
        // 获取 ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 获取 repositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        // 使用service 进行流程部署, 定义流程名 将bpmn和png部署到数据库中
        Deployment deploy = repositoryService.createDeployment()
                .name("出差申请")
                .addClasspathResource("bpmn/evection.bpmn")
                .addClasspathResource("bpmn/evection.png")
                .deploy();
        System.out.println("流程id: " + deploy.getId());
        System.out.println("流程名称:" + deploy.getName());

    }
    /**
     * 启动流程实例
     */
    @Test
    public void startProcess() {

        // 获取 ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //　获取runTime service
        RuntimeService runtimeService = processEngine.getRuntimeService();
        // 根据流程定义的id启动实例
        ProcessInstance instance = runtimeService.startProcessInstanceByKey("myEvection");
        System.out.println("流程定义id:" + instance.getProcessDefinitionId() );
        System.out.println("流程实例id:" + instance.getId());
        System.out.println("当前活动id:" + instance.getActivityId());
    }

    /**
     * 查询个人待执行的任务
     */
    @Test
    public void  findPersionTask() {
        // 获取 ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //　获取taskService
        TaskService taskService = processEngine.getTaskService();
        // 根据流程key和任务负责人查询任务
        List<Task> list = taskService.createTaskQuery().processDefinitionKey("myEvection")
                .taskAssignee("jerry")
                .list();
        list.forEach(task -> {
            System.out.println("流程实例id：" + task.getProcessInstanceId());
            System.out.println("任务id：" + task.getId());
            System.out.println("任务负责人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
        });


    }

    @Test
    public void queryProcessDefinition(){
        //        获取引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        repositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
//        得到ProcessDefinitionQuery 对象
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
//          查询出当前所有的流程定义
//          条件：processDefinitionKey =evection
//          orderByProcessDefinitionVersion 按照版本排序
//        desc倒叙
//        list 返回集合
        List<ProcessDefinition> definitionList = processDefinitionQuery.processDefinitionKey("myEvection")
                .orderByProcessDefinitionVersion()
                .desc()
                .list();
//      输出流程定义信息
        for (ProcessDefinition processDefinition : definitionList) {
            System.out.println("流程定义 id="+processDefinition.getId());
            System.out.println("流程定义 name="+processDefinition.getName());
            System.out.println("流程定义 key="+processDefinition.getKey());
            System.out.println("流程定义 Version="+processDefinition.getVersion());
            System.out.println("流程部署ID ="+processDefinition.getDeploymentId());
        }

    }

    /**
     * 流程处理
     */
    @Test
    public void completeTask() {
        // 获取流程引擎和taskService
        ProcessEngine processEngines = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngines.getTaskService();
        Task task = taskService.createTaskQuery().processDefinitionKey("myEvection").taskAssignee("rose").singleResult();
        System.out.println("流程实例id：" + task.getProcessInstanceId());
        System.out.println("任务id：" + task.getId());
        System.out.println("任务负责人：" + task.getAssignee());
        System.out.println("任务名称：" + task.getName());
        taskService.complete(task.getId());
    }

    /**
     * 删除部署流程
     */
    @Test
    public void removeDeployment() {
        // act_re_deployment 主键
        String deploymentId = "1";
        // 获取 ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        // 当流程没有完成,需要删除流程时 可以用级联删除, 设置第二个参数为true,会删除历史数据
        //  repositoryService.deleteDeployment(deploymentId, true);
        repositoryService.deleteDeployment(deploymentId);
    }
}
